﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour {

	public GameObject hazard;
	public Vector3 spawnValues;

	public int hazardCount;
	public float spawnWait; // Interval btn two spawnings
	public float startWait; // Waiting time before spawning begins
	public float waveWait;	// Interval btn two waves of spawnings

	public GUIText scoreText;
	private int score;

	// Spawning the hazards
	IEnumerator SpawnWaves () {
		yield return new WaitForSeconds (startWait);
		// Spawn infinite number of hazards
		while(true) {
			for(int i = 0; i < hazardCount; ++i) {
				Vector3 spawnPos = new Vector3 (Random.Range(-spawnValues.x, spawnValues.x),/* Can't use
				a single value for x because an asteroid is expected to spawn at any visible pos along
				the x-axis */ spawnValues.y, spawnValues.z);
				Quaternion spawnRot = Quaternion.identity; // No rotation by default
				Instantiate (hazard, spawnPos, spawnRot);	
				yield return new WaitForSeconds (spawnWait);
			}	
			yield return new WaitForSeconds (waveWait);
		}
	}

	public void addScore(int newScoreValue) 
	{
		score += newScoreValue;
		updateScore ();
	}
	void updateScore() 
	{
		scoreText.text = "Score" + score; // Unity automatically converts int to string
	}

	// Use this for initialization
	void Start () {
		score = 0;
		updateScore ();
		StartCoroutine(SpawnWaves());	// Coroutines can be interrupted and continue at the next frame
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
